package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.RemoteStrategyService;
import cn.wolfcode.wolf2w.business.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.api.domain.StrategyEs;
import cn.wolfcode.wolf2w.business.repository.StrategyEsRepository;
import cn.wolfcode.wolf2w.business.service.IStrategyEsService;
import cn.wolfcode.wolf2w.common.core.domain.R;
import com.alibaba.fastjson2.JSON;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyEsServiceImpl implements IStrategyEsService {

    @Autowired
    private StrategyEsRepository repository;

    @Autowired
    private RemoteStrategyService remoteStrategyService;

    @Override
    public void initStrategyEs() {
        //发送请求到攻略服务器请求攻略数据
        R<List<Strategy>> r = remoteStrategyService.list("inner");
        List<Strategy> strategyList = r.getData();
        for (Strategy strategy : strategyList) {
            StrategyEs es = new StrategyEs();
            //属性拷贝 把同名属性拷贝到新对象中
            BeanUtils.copyProperties(strategy,es);
            repository.save(es);
        }
        //将攻略数据转换为Es数据的类型

        //存储到Es库中
    }

    @RabbitListener(queues = {"travelQueue"})
    public void onMessage(String message){
        Strategy strategy = JSON.parseObject(message, Strategy.class);
        StrategyEs es = new StrategyEs();
        BeanUtils.copyProperties(strategy,es);
        repository.save(es);
    }
}
